package com.cqjtu.education.controller;

import com.cqjtu.education.entity.Course;
import com.cqjtu.education.entity.Notice;
import com.cqjtu.education.service.NoticeService;
import com.cqjtu.education.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Notice)表控制层
 *
 * @author makejava
 * @since 2022-06-09 19:09:03
 */
@RestController
@Api(tags = "通知相关接口")
@RequestMapping("notice")
public class NoticeController {
    /**
     * 服务对象
     */
    @Resource
    private NoticeService noticeService;

    /**
     * 根据课程id查询公告数目
     *
     * @param courseId 课程id
     * @return 数量
     */
    @RequestMapping("/count")
    @ApiOperation(value = "count", notes = "查询指定课程对应的公告数")
    public Result count(String courseId) {
        return Result.success(this.noticeService.count(courseId));
    }

    /**
     * 分页查询
     *
     * @param pageNum  页数
     * @param pageSize 大小
     * @return 查询结果
     */
    @RequestMapping("/queryByPage")
    @ApiOperation(value = "queryByPage", notes = "分页查询")
    public Result queryByPage(String courseId, String pageNum, String pageSize) {
        List<Notice> list = new ArrayList<>();
        try {
            list = this.noticeService.queryByPage(courseId, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("发生异常");
        }
        return Result.success(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "通过主键查询单个数据")
    public Result queryById(String id) {
        return Result.success(this.noticeService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param notice 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    @ApiOperation(value = "add", notes = "新增公告")
    public Result add(@RequestBody Notice notice) {
        try {
            this.noticeService.insert(notice);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("新增失败");
        }
        return Result.success(notice);
    }

    /**
     * 编辑数据
     *
     * @param notice 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "修改公告")
    public Result edit(@RequestBody Notice notice) {
        try {
            this.noticeService.update(notice);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("修改失败");
        }
        return Result.success(notice);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/deleteById")
    @ApiOperation(value = "deleteById", notes = "删除公告")
    public Result deleteById(String id) {
        return Result.success(this.noticeService.deleteById(id));
    }

}

